package com.zrkj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zrkj.entity.TUser;
import com.zrkj.entity.dto.UserDto;
import com.zrkj.entity.vo.LoginVo;
import com.zrkj.entity.vo.Result;
import com.zrkj.service.ITUserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : 国良
 * @className : TUserController
 * @package: com.wg.controller
 * @description :  前端控制器
 * @date :  2021-03-04
 */
@RestController
@RequestMapping(value = "/tUser")
@Api(tags = "用户管理")
public class TUserController {
    private final ITUserService itUserService;

    public TUserController(ITUserService itUserService) {
        this.itUserService = itUserService;
    }

    /**
     * 添加
     *
     * @param tUser
     * @return
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody TUser tUser) {
        return itUserService.add(tUser);
    }

    /**
     * 更新
     *
     * @param tUser
     * @return
     */
    @PutMapping(value = "/update")
    public Result<Boolean> update(@RequestBody TUser tUser) {
        return itUserService.update(tUser);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<Boolean> delete(@RequestParam(value = "ids") String ids) {
        String[] idsArray = ids.split(",");
        List<String> idList = new ArrayList<>();
        for (String id : idsArray) {
            idList.add(id);
        }
        return itUserService.delete(idList);
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getById/{id}")
    public Result<TUser> getById(@PathVariable(value = "id") String id) {
        return itUserService.getById(id);
    }

    /**
     * 查询列表
     *
     * @param tUser
     * @return
     */
    @PostMapping(value = "/queryList")
    public Result<List<TUser>> queryList(@RequestBody TUser tUser) {
        return itUserService.queryList(tUser);
    }

    /**
     * 查询分页列表
     *
     * @param tUser
     * @return
     */
    @PostMapping(value = "/queryPageList")
    public Result<IPage<TUser>> queryPageList(@RequestBody UserDto tUser) {

        return itUserService.queryPageList(tUser.getPage(), tUser);
    }


    @GetMapping(value = "/login")
    public Result login(@RequestParam(value = "iotLogin",defaultValue = "0") String iotLogin, @RequestParam("username") String username, @RequestParam("qaz") String password) {

        return itUserService.login(iotLogin, username, password);
    }

    @PostMapping(value = "/getResources")
    public Result getResources(@RequestBody LoginVo loginVo) {
        return itUserService.getResources(loginVo);
    }
}
